﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using aaaSoft.SkinEngine.SkinHelpers;

namespace aaaSoft.SkinEngine.MyControls
{
    class MyButton : IMyControl
    {
        Button btnBase;
        SkinEngine skinEng;
        private Bitmap _LightImage = new Bitmap(40, 20);

        private bool IsMouseDown = false;
        private bool IsMouseOn = false;
        private int BorderWidth = 1;
        private int RoundRectWidth = 1;

        public MyButton(Button btn, SkinEngine skinEng)
        {
            btnBase = btn;
            this.skinEng = skinEng;

            //得到光亮图像
            Rectangle rect = new Rectangle(0, 0, this._LightImage.Width / 2, this._LightImage.Height);
            Rectangle rectangle2 = new Rectangle(this._LightImage.Width / 2, 0, this._LightImage.Width / 2, this._LightImage.Height);
            Graphics graphics = Graphics.FromImage(this._LightImage);
            LinearGradientBrush brush = new LinearGradientBrush(rect, Color.FromArgb(0, 0xff, 0xff, 0xff), Color.FromArgb(150, 0xff, 0xff, 0xff), LinearGradientMode.Horizontal);
            graphics.FillRectangle(brush, rect);
            brush = new LinearGradientBrush(rectangle2, Color.FromArgb(150, 0xff, 0xff, 0xff), Color.FromArgb(0, 0xff, 0xff, 0xff), LinearGradientMode.Horizontal);
            graphics.FillRectangle(brush, rectangle2);
        }

        public void StartControlSkin()
        {
            btnBase.Paint += new PaintEventHandler(btnBase_Paint);
            btnBase.MouseHover += new EventHandler(btnBase_MouseHover);
            btnBase.MouseLeave += new EventHandler(btnBase_MouseLeave);
            btnBase.MouseDown += new MouseEventHandler(btnBase_MouseDown);
            btnBase.MouseUp += new MouseEventHandler(btnBase_MouseUp);
        }

        public void StopControlSkin()
        {
            btnBase.Paint -= new PaintEventHandler(btnBase_Paint);
            btnBase.MouseHover -= new EventHandler(btnBase_MouseHover);
            btnBase.MouseLeave -= new EventHandler(btnBase_MouseLeave);
            btnBase.MouseDown -= new MouseEventHandler(btnBase_MouseDown);
            btnBase.MouseUp -= new MouseEventHandler(btnBase_MouseUp);
            
            //停止时鼠标状态回复初始化
            IsMouseDown = false;
            IsMouseOn = false;
        }
        
        //鼠标按下时
        void btnBase_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                IsMouseDown = true;
                btnBase.Invalidate();    
            }
        }

        //鼠标弹起时
        void btnBase_MouseUp(object sender, MouseEventArgs e)
        {
            IsMouseDown = false;
            btnBase.Invalidate();
        }

        //鼠标进入时
        void btnBase_MouseHover(object sender, EventArgs e)
        {
            IsMouseOn = true;
            btnBase.Invalidate();
        }

        //鼠标离开时
        void btnBase_MouseLeave(object sender, EventArgs e)
        {
            IsMouseOn = false;
            btnBase.Invalidate();
        }

        void btnBase_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            DrawRectButton(g);
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            sf.FormatFlags = StringFormatFlags.NoWrap;

            SolidBrush sb = null;

            RectangleF rect = new RectangleF(new Point(0, 0), btnBase.Size);
            string displayText = SkinHelpers.GraphicHelper.GetAppropriateString(g, btnBase.Text.Replace("&", string.Empty), btnBase.Font, rect);
            if (btnBase.Enabled)
            {
                sb = new SolidBrush(btnBase.ForeColor);
            }
            else
            {
                sb = new SolidBrush(SkinHelpers.ColorHelper.GetDeeperColor(Color.White, 0.9F));
                g.DrawString(displayText, btnBase.Font, sb, rect, sf);

                sb = new SolidBrush(SkinHelpers.ColorHelper.GetDeeperColor(skinEng.BackColor, 0.6F));
            }
            g.DrawString(displayText, btnBase.Font, sb, rect, sf);
        }

        private int LightWidth
        {
            get
            {
                return Convert.ToInt32((double)(((double)(btnBase.Width * 3)) / 4.0));
            }
        }

        //画矩形按钮
        private void DrawRectButton(Graphics graphics)
        {
            float deep = 2f;
            float num2 = 0.9f;
            float num3 = 0.8f;
            float num4 = 1f;
            if (this.IsMouseOn)
            {
                num2 = 0.8f;
                num3 = 0.7f;
                num4 = 0.9f;
            }
            if (this.IsMouseDown)
            {
                num2 = 0.7f;
                num3 = 0.6f;
                num4 = 0.8f;
            }
            Rectangle rect = new Rectangle(0, 0, btnBase.Width, Convert.ToInt32((double)(((double)btnBase.Height) / 3.0)) + 1);
            LinearGradientBrush brush = new LinearGradientBrush(rect, ColorHelper.GetDeeperColor(skinEng.BackColor, deep), ColorHelper.GetDeeperColor(skinEng.BackColor, num2), LinearGradientMode.Vertical);
            graphics.FillRectangle(brush, brush.Rectangle);
            rect = new Rectangle(0, Convert.ToInt32((double)(((double)btnBase.Height) / 3.0)), btnBase.Width, btnBase.Height - Convert.ToInt32((double)(((double)btnBase.Height) / 3.0)));
            brush = new LinearGradientBrush(rect, ColorHelper.GetDeeperColor(skinEng.BackColor, num3), ColorHelper.GetDeeperColor(skinEng.BackColor, num4), LinearGradientMode.Vertical);
            graphics.FillRectangle(brush, brush.Rectangle);
            brush.Dispose();
            if (btnBase.Image != null)
            {
                graphics.DrawImage(btnBase.Image, new RectangleF(new Point(0, 0), btnBase.Size));
            }
            if (this.IsMouseOn | this.IsMouseDown)
            {
                Bitmap image = new Bitmap(this._LightImage, this.LightWidth, btnBase.Height);
                graphics.DrawImage(image, (btnBase.Width - this.LightWidth) / 2, 0);
                image.Dispose();
            }
            Pen pen = new Pen(ColorHelper.GetDeeperColor(skinEng.BackColor, 0.9f), (float)this.BorderWidth);
            rect = new Rectangle(0, 0, btnBase.Width - 1, btnBase.Height - 1);
            graphics.DrawRectangle(pen, rect);
            graphics.DrawArc(pen, 0, 0, this.RoundRectWidth, this.RoundRectWidth, -180, 90);
            graphics.DrawArc(pen, (btnBase.Width - this.RoundRectWidth) - 2, 0, this.RoundRectWidth, this.RoundRectWidth, -90, 90);
            graphics.DrawArc(pen, (btnBase.Width - this.RoundRectWidth) - 2, (btnBase.Height - this.RoundRectWidth) - 2, this.RoundRectWidth, this.RoundRectWidth, 0, 90);
            graphics.DrawArc(pen, 0, (btnBase.Height - this.RoundRectWidth) - 2, this.RoundRectWidth, this.RoundRectWidth, 90, 90);
            pen.Dispose();
        }
    }
}
